
layui.define(['form','element','scene'], function(exports) {
	let $ = layui.jquery,form = layui.form,scene = layui.scene,element = layui.element;

    layui['api_list_change'] = function (id) {
        console.log(id)
    }

	function tabshow(id){
        let arr = id.split('/');
        let did =  (['tab'].concat(arr)).join('-');
        let view = '#'+ did;

        if(!layui['game/main'] || !layui['game/main']['apis'] || !layui['game/main']['apis'][id]){
            return $(view).html('api not exist');
        }
        let data = {},apis = layui['game/main']['apis'][id];
        data['formid'] = arr.join('-');
        data['method'] = [];
        for(let k in apis['method']){
            data['method'].push({"id":k,"name":apis['method'][k]['desc'][0]||k  })
        }
        if(data['method'].length < 1){
            data['method'].push({"id":'',"name":'请选择' })
        }
        let val = arguments[1]||data['method'][0]['id'];
        data['param'] = [];
        if(apis['method'][val]){
            data['param'] = apis['method'][val]['param'];
        }
        data['tip']=apis['method'][val]['tip'];
        let server,apiname = [id,val].join('/');
        console.log("妈的必然是这里有问题啊----------apiname:",apiname)
        scene.gamesdk.game.server().then(ret=>{
            server = ret;
            return scene.view('show',data);
        }).then(ret=>{
            $(view).html(ret);
            form.render(null,did);
            form.val(did, {
                "api_list": val,
                "api_name" : apiname,
                "api_url" : server['remote'] + '/s' + server['_id'] + apiname,
            })
            form.on('select(select'+data['formid']+')', function(d){
                tabshow(id,d['value'])
            });
            form.on('submit(submit'+data['formid']+')', function () {
                let args = arguments[0].field;
                let apiname = args['api_name'];
                for(let k of ['api_list','api_name','api_url','api_result']){
                    delete args[k];
                }
                scene.gamesdk.game.send(apiname,args).then(ret=>{
                    form.val(did, {  "api_result" : JSON.stringify(ret) })
                }).catch(err=>{
                    form.val(did, {  "api_result" : JSON.stringify(err) })
                })
                return false;
            })
        }).catch(err=>{
            $(view).html(err.toString());
        })
    }

    function tabAdd(api,title) {
        //判断当前id的元素是否存在于tab中
        let id = api.split('/').join('-');
        let li = $(".layui-tab li[lay-id=" + id + "]").length;
        //console.log(li);
        if(li > 0) {
            element.tabChange('tab1', id);
        } else {
            let did = ["tab",id].join('-');
        	let content = '<div id="'+did+'" class="layui-form" lay-filter="'+did+'">loading...</div>';
            element.tabAdd('tab1', { id: id,title: title, content: content });
            element.tabChange('tab1',id);
            tabshow(api)
        }
    }

	function render() {
		$('.layui-nav dd').click(function (event) {
			let A = $(this).children('a');
            let api = A.attr('api')||'',title = A.html();
            if(api){
                tabAdd(api,title);
            }
			event.stopPropagation(); //不触发任何前辈元素上的事件处理函数
		});
	}

	exports('admin', render);
});